<?php
declare (strict_types = 1);

namespace app\index\model;

/**
 * 专门获取侧边栏数据
 */
class Block
{
    const USER_NUMBER = 6; //侧边栏显示作者个数
    const ARTICLE_NUMBER = 8;//侧边栏显示文章数目
    const TAGS_NUMBER = 12;//侧边栏显示标签个数



    /**
     * #查询构造器
     * @return Article
     */
    private static function getArticleQuery(){
        return Article::where([
            ['create_time', '>', strtotime('-3 month')],
            ['status','=', Article::STATUS_PUB]
        ]);
    }

    /**
     * 获取活跃文章id
     * @param string $field
     * @param array $where
     * @return array
     */
    public static function getActiveId(string $field, array $where=[]){
        $cacheKey = json_encode(array_merge((array)$field, $where));

        if (!$ids = cache($cacheKey)){
            $article = static::getArticleQuery()
                ->where($where)
                ->group($field)
                ->order(['count'=>'DESC'])
                ->column($field . ',count(id) as count', $field);
            $ids = array_keys($article);
            cache($cacheKey, $ids, 24 * 3600);
        }

        return $ids;
    }

    /**
     * 获取活跃用户
     */
    public static function getActiveAuthors(){
        if(!$authors = cache('activeAuthors')){
            $authors = User::where([
                'id' => static::getActiveId('user_id')
            ])
                ->order(['update_time'=>'DESC'])
                ->field('id,username,nickname,avatar')
                ->withCount(['topics','articles'])
                ->limit(static::USER_NUMBER)
                ->select()
                ->each(function($item, $k){
                    $item->avatarUrl = $item->avatarUrl;
                    $item->showName = $item->showName;
                })->toArray();
            cache('activeAuthors', $authors, 12 * 3600);
        }

        return $authors;
    }

    /**
     * 获取活跃话题
     */
    public static function getActiveTopics(){
        if(!$topics = cache('activeTopics')){
            $topics = Topic::where([
                'id'=> static::getActiveId('topic_id',[
                    ['topic_id','>',0]
                ])
            ])
                ->cache(12 * 3600)
                ->field(['id','name','image','desc'])
                ->order(['update_time'=>'DESC','id'=>'DESC'])
                ->select()
                ->each(function($item, $k){
                    $item->url = (string)url('topic/read',['id'=>$item->id]);
                    $item->imageUrl = (string)$item->imageUrl;
                })
                ->toArray();
            cache('activeTopics', $topics, 12 * 3600);
        }

        return $topics;
    }

    /**
     * 获取活跃文章
     */
    public static function getActiveArticles(){
        if (!$articles = cache('activeArticles')){
            $articles = static::getArticleQuery()
                ->field(['id','title','image','visited'])
                ->order(['visited'=>'DESC'])
                ->limit(static::ARTICLE_NUMBER)
                ->select()
                ->each(function($item, $k){
                    $item->imageUrl = (string) $item->imageUrl;
                })
                ->toArray();
            cache('activeArticles',$articles,12 * 3600);
        }
        return $articles;
    }

    /**
     * 获取最新标签
     */
    public static function getActiveTags(){
        if(!$tags = cache('activeTags')){
            $tags = Tag::field(['id','name'])
                ->order('id', 'desc')
                ->limit(static::TAGS_NUMBER)
                ->select()
                ->toArray();
            cache('activeTags', $tags, 12 * 3600);
        }
        return $tags;
    }






}
